package paginas;

import java.awt.Dimension;
import java.awt.Font;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Image;
import java.awt.event.KeyEvent;
import java.io.File;
import java.util.ResourceBundle;

import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JPasswordField;
import javax.swing.JTextField;
import javax.swing.border.TitledBorder;

import modelUtil.Util;
import usuario.usuarioTO.UsuarioTO;
import util.BotonPersonalizado;
import AccionBoton.AccionFocoCreditos;
import AccionBoton.AccionSeleccionImagen;

import com.sun.java.swing.plaf.nimbus.LoweredBorder;

public class MenuUsuarioDatosTab extends JPanel{



	private static final long serialVersionUID = 1L;
	private JPanel panel1;
	private JPanel panel2;
	private JPanel panel3;
	private JTextField texto1 = new JTextField();
	private JLabel imagenMostrada = new JLabel();
	private File imagen;
	private UsuarioTO _user;

	public MenuUsuarioDatosTab(UsuarioTO user){
		_user = user;
		this.setPreferredSize(new Dimension(500, 450));
		panel1 = new JPanel();
		panel2 = new JPanel();
		panel3 = new JPanel();
		escogerDatos();
		initComponents();
		
	}
	
	
	public MenuUsuarioDatosTab(){
		
		this.setPreferredSize(new Dimension(500, 450));
		panel1 = new JPanel();
		panel2 = new JPanel();
		panel3 = new JPanel();
		initComponents();
		
	}
	
	private void initComponents(){
		
		
		GridBagLayout grid = new GridBagLayout();
		GridBagConstraints cons = new GridBagConstraints();
		cons.gridheight = 1;
		cons.gridwidth = 1;
		cons.gridx = 0;
		cons.gridy = 0;
		cons.fill = GridBagConstraints.BOTH;
		this.setLayout(grid);
		crearPanel1();
		grid.addLayoutComponent(panel1, cons);
		this.add(panel1);
		crearPanel2();
		cons.gridy = 1;
		grid.addLayoutComponent(panel2, cons);
		this.add(panel2);
		crearPanel3();
		cons.gridy = 2;
		grid.addLayoutComponent(panel3, cons);
		this.add(panel3);
	}
	
	private void crearPanel1(){
		panel1.setPreferredSize(new Dimension(450,150));
		TitledBorder border = new TitledBorder(ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_menu_usuarios_datos_titulo_nombre"));
		border.setTitleFont(new Font("Verdana", Font.BOLD, 18));
		panel1.setBorder(border);
		GridBagLayout grid = new GridBagLayout();
		GridBagConstraints cons = new GridBagConstraints();
		panel1.setLayout(grid);
		cons.gridheight = 1;
		cons.gridwidth = 1;
		cons.gridx = 0;
		cons.gridy = 0;
		JLabel titulo = new JLabel(ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_menu_usuarios_datos_nombre_usuario"));
		grid.addLayoutComponent(titulo, cons);
		panel1.add(titulo);
		
		cons.gridx = 0;
		cons.gridy = 1;
		JLabel titulo2 = new JLabel(ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_menu_usuarios_datos_nombre"));
		grid.addLayoutComponent(titulo2, cons);
		panel1.add(titulo2);
		
		cons.gridx = 1;
		cons.gridy = 1;
		texto1.setPreferredSize(new Dimension(125,25));
		texto1.getAccessibleContext().setAccessibleName(ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_accesible_seleccion_nombre_usuario"));
		texto1.setToolTipText(ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_accesible_seleccion_nombre_usuario"));
		grid.addLayoutComponent(texto1, cons);
		panel1.add(texto1);
		panel1.setVisible(true);
				
	}
	
	private void crearPanel2(){
	 BotonPersonalizado boton1 = Util.BotonImagen(ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_menu_usuario_seleccion_imagen"), "");
	 boton1.addActionListener(new AccionSeleccionImagen(this));
	 boton1.addFocusListener(new AccionFocoCreditos(boton1));
	 boton1.setMnemonic(KeyEvent.VK_S);
	 boton1.getLabelTexto().setDisplayedMnemonic(KeyEvent.VK_S);
	 boton1.getLabelTexto().getAccessibleContext().setAccessibleName(ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_accesible_seleccion_imagen_usuario"));
	 boton1.setToolTipText(ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_accesible_seleccion_imagen_usuario"));
	 panel2.add(boton1);
		
	}
	
	private void crearPanel3(){
		
		TitledBorder border = new TitledBorder(ResourceBundle.getBundle("internacionalizacion/Etiquetas").getString("etiqueta_menu_usuario_titulo_imagen"));
		border.setTitleFont(new Font("Verdana", Font.BOLD, 18));
		panel3.setBorder(border);
		GridBagLayout grid = new GridBagLayout();
		GridBagConstraints cons = new GridBagConstraints();
		panel3.setLayout(grid);
		cons.gridheight = 1;
		cons.gridwidth = 1;
		cons.gridx = 0;
		cons.gridy = 0;
		
		panel3.setPreferredSize(new Dimension(450,200));
		grid.addLayoutComponent(imagenMostrada, cons);
		panel3.add(imagenMostrada);
		panel3.setVisible(true);

		
	}
	

	public JPanel getPanel3() {
		return panel3;
	}

	public void setPanel3(JPanel panel3) {
		this.panel3 = panel3;
	}

	public JTextField getTexto1() {
		return texto1;
	}

	public void setTexto1(JPasswordField texto1) {
		this.texto1 = texto1;
	}

	public File getImagen() {
		return imagen;
	}

	public void setImagen(File imagen) {
		this.imagen = imagen;
	}

	public JLabel getImagenMostrada() {
		return imagenMostrada;
	}

	public void setImagenMostrada(JLabel imagenMostrada) {
		this.imagenMostrada = imagenMostrada;
	}

	public void escogerDatos(){
		
		texto1.setText(_user.getNombre());
		
        File imagen = new File(_user.getImagen());
		JLabel foto = imagenMostrada;
		foto.setBorder(new LoweredBorder());
		ImageIcon im = new ImageIcon(imagen.getPath());
		int alto = im.getIconHeight();
		double escala = (double)alto/80;
		int ancho = (int)(im.getIconWidth()/escala);
		Image redimensionada = im.getImage().getScaledInstance(ancho,
				80, Image.SCALE_DEFAULT);
		foto.setIcon(new ImageIcon(redimensionada));
		foto.setPreferredSize(new Dimension(foto.getIcon().getIconWidth()+15,foto.getIcon().getIconHeight()+15));
		imagenMostrada = foto;
		imagenMostrada.setPreferredSize(new Dimension(foto.getIcon().getIconWidth()+15,foto.getIcon().getIconHeight()+15));

		this.getPanel3().setVisible(false);
		this.getPanel3().setVisible(true);
		
	}


	public UsuarioTO get_user() {
		return _user;
	}
	
	
}
